package ui

import (
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
)

// ShowRemoteDialog 显示远程仓库选择对话框
func ShowRemoteDialog(owner walk.Form, remotes map[string]string) (name, url string, ok bool) {
	var dlg *walk.Dialog
	var lb *walk.ListBox
	var items []string

	// 收集远程仓库名称
	for n := range remotes {
		items = append(items, n)
	}

	if len(items) == 0 {
		walk.MsgBox(owner, "提示", "没有找到远程仓库", walk.MsgBoxIconInformation)
		return "", "", false
	}

	// 创建对话框
	dialogConfig := Dialog{
		AssignTo: &dlg,
		Title:    "选择远程仓库",
		MinSize:  Size{Width: 200, Height: 300},
		Layout:   VBox{},
		Children: []Widget{
			ListBox{
				AssignTo: &lb,
				Model:    items,
			},
			PushButton{
				Text: "确定",
				OnClicked: func() {
					if idx := lb.CurrentIndex(); idx != -1 {
						name = items[idx]
						url = remotes[name]
						ok = true
						dlg.Accept()
					}
				},
			},
		},
	}

	if err := dialogConfig.Create(owner); err != nil {
		walk.MsgBox(owner, "错误", "创建对话框失败: "+err.Error(), walk.MsgBoxIconError)
		return "", "", false
	}

	dlg.Run()
	return
}
